ReentrantLock
内部使用了AQS:https://blog.csdn.net/u010900754/article/details/88849938
但是因为有不同的锁模式,用了两种具体实现分别应对公平锁和非公平锁。
二者在实现获取锁的逻辑上有区别,也就是AQS留空的部分。下面看下lock和unlock实现。
非公平:
static final class NonfairSync extends Sync {
private static final long serialVersionUID = 7316153563782823691L;
/**
* Performs lock. Try immediate barge, backing up to normal
* acquire on failure.
*/
final void lock() {
if (compareAndSetState(0, 1))
setExclusiveOwnerThread(Thread.currentThread());
else
acquire(1);
}
protected final boolean tryAcquire(int acquires) {
return nonfairTryAcquire(acquires);
}
}
这里先快速获取一次,失败就走ASQ的过程。acquire方法会走到tryAcquire,tryAcquire调用了父类的nonfairTryAcquire。
final boolean nonfairTryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
if (c == 0) {
if (compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0) // overflow
throw new Error("Maximum lock count exceeded");
setState(nextc);
return true;
}
return false;
}
先判断AQS的state是否是0,是0说明没有现成持有锁,CAS获取一次,不行就走AQS的逻辑。如果state非0,也不是说不能获取,因为是“重入锁”嘛,所以再判断一次当前线程是否是自己,如果是,可以重入,直接吧state++即可。这里非公平体现在哪里?当前线程总是可以通过tryAcquire直接快速获取锁,即使等待队列里有其他线程等待,所以是非公平的。另外还可以支持重入。这也是为什么AQS代码把获取这部分逻辑留空的原因。
下面是release的实现。仅仅是设置了state,队列操作在AQS里面。
protected final boolean tryRelease(int releases) {
int c = getState() - releases;
if (Thread.currentThread() != getExclusiveOwnerThread())
throw new IllegalMonitorStateException();
boolean free = false;
if (c == 0) {
free = true;
setExclusiveOwnerThread(null);
}
setState(c);
return free;
}
公平锁:
static final class FairSync extends Sync {
private static final long serialVersionUID = -3000897897090466540L;
final void lock() {
acquire(1);
}
/**
* Fair version of tryAcquire. Don't grant access unless
* recursive call or no waiters or is first.
*/
protected final boolean tryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
if (c == 0) {
if (!hasQueuedPredecessors() &&
compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0)
throw new Error("Maximum lock count exceeded");
setState(nextc);
return true;
}
return false;
}
}
}
可以看到获取锁必须通过hasQueuedPredecessors的检验:
public final boolean hasQueuedPredecessors() {
// The correctness of this depends on head being initialized
// before tail and on head.next being accurate if the current
// thread is first in queue.
Node t = tail; // Read fields in reverse initialization order
Node h = head;
Node s;
return h != t &&
((s = h.next) == null || s.thread != Thread.currentThread());
}
这个函数只有在要么head.next是空要么head.next所代表的的线程不是当前线程才返回true,这种情况意味着当前线程不是队列的最前面,只有在最前面才可以尝试获取锁。